﻿using Cms.Core.Infrastructure.Ui;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cms.Core.Data.UnitOfWork
{
    public interface IUnitOfWork
    {
        void BeginTransaction();

        void Commit(Action action);

        OperationResult Commit(Func<OperationResult> func);
        void Commit();

         Task<OperationResult> Commit(Func<Task<OperationResult>> func);

        /// <summary>释放对象.</summary>
        void Dispose();


        IDbContextTransaction GetTransaction();
        IDbContextTransaction GetTransaction(IsolationLevel isolationLevel);
        Task<IDbContextTransaction> GetTransactionAsync(CancellationToken cancellationToken = default(CancellationToken));
        Task<IDbContextTransaction> GetTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken = default(CancellationToken));

        DatabaseFacade Database();
        DbContext GetContext();

        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        IEnumerable<T> SqlQuery<T>(string sql, Dictionary<string, object> paraDic) where T : class, new();


        /// <summary>
        /// sql查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        IEnumerable<dynamic> SqlQuery(string sql, Dictionary<string, object> paraDic);



        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        Task<IEnumerable<T>> SqlQueryAsync<T>(string sql, Dictionary<string, object> paraDic) where T : class, new();
    }
}
